package com.activities;


import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.util.TypedValue;
import android.widget.GridView;

import com.adaptadores.LugaresThumbAdapter;
import com.callbacks.Callbacks.LCompleted;
import com.callbacks.InterfaceLugares;
import com.clases.Categoria;
import com.clases.Lugar;
import com.constantes.AppConstant;
import com.constantes.Constantes;
import com.example.santiagotur.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.util.Utils;


public class Lugares_Lista extends Activity implements InterfaceLugares {

    private LugaresThumbAdapter adapter;

    private int columnWidth;

    private GridView gridView;

    private void config(Context context) {

        File cacheDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()
                    + "SantiagoTur/");

        if (!cacheDir.exists())
            cacheDir.mkdir();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPoolSize(3) // default
        .threadPriority(Thread.NORM_PRIORITY - 2) // default
        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
        .writeDebugLogs().denyCacheImageMultipleSizesInMemory().discCache(new UnlimitedDiscCache(cacheDir)) // default
        .build();

        if (ImageLoader.getInstance().isInited())
            ImageLoader.getInstance().destroy();

        ImageLoader.getInstance().init(config);
    }

   

    private void inicializar() {

        config(this);

        gridView = (GridView) findViewById(R.id.gridView_MejorLugar);

        initilizeGridLayout();

        getLugares(0);

    }

    private void initilizeGridLayout() {

        Resources r = getResources();
        float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, AppConstant.GRID_PADDING, r.getDisplayMetrics());

        Utils utils = new Utils(this);
        columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

        gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
        gridView.setColumnWidth(columnWidth);
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setPadding((int) padding, (int) padding, (int) padding, (int) padding);
        gridView.setHorizontalSpacing((int) padding);
        gridView.setVerticalSpacing((int) padding);

    }

    private void procesarLugares(Lugar[] result) {

        if (adapter == null) {
            adapter = new LugaresThumbAdapter(this, columnWidth,this);
            gridView.setAdapter(adapter);
        }

        for (Lugar lg : result) {
            adapter.add(lg);
        }

        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.lugares);
    }

    @Override
    protected void onResume() {

        inicializar();
        super.onResume();
    }

	@Override
	public void getLugares(int last) {
		  Lugar lg = new Lugar(this);
	        lg.setCallbackLugar(new LCompleted() {

	            @Override
	            public void Completed(Lugar[] result) {

	                procesarLugares(result);

	            }
	        });
	        lg.setLast(gridView.getChildCount());
	        Categoria cat = new Categoria();

	        cat.setIDCategoria(3);
	        lg.setCategoria(cat);
	        lg.execute(Constantes.ACCION_LUGAR_GET_CATEGORIA);
		
	}

}
